package com.example.malltest0.controller;


import com.example.malltest0.common.api.CommonResult;
import com.example.malltest0.mbg.model.Followers;
import com.example.malltest0.mbg.model.Platform;
import com.example.malltest0.service.PlatformService;
import com.example.malltest0.service.RedisService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/platform")
@Api(tags = "PlatformController", description = "平台管理")
public class PlatformController {

    @Autowired
    private PlatformService platformService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加平台")
    @PreAuthorize("hasAuthority('pms:brand:read')")
    public CommonResult addPlatform(@RequestBody Platform platform){
        int count=platformService.addPlatform(platform);

        if(count>0) {

            return CommonResult.success("添加成功");

        }
        return CommonResult.failed("已经存在该平台");
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation("修改平台")
    @PreAuthorize("hasAuthority('pms:brand:read')")
    public CommonResult updatePlatform(@RequestBody Platform platform){
        platformService.updatePlatform(platform);
        return CommonResult.success("修改成功");
    }


    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ApiOperation("删除平台")
    @PreAuthorize("hasAuthority('pms:brand:read')")
    public CommonResult deletePlatform(@RequestParam Long id){
        platformService.deletePlatform(id);
        return CommonResult.success("删除成功");
    }

    @ResponseBody
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    @ApiOperation("查询平台")
    @PreAuthorize("hasAuthority('pms:brand:read')")
    public CommonResult<PageInfo<Platform>> selectAllPlatform(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Platform> list=platformService.selectAllPlatform();
        if(list!=null){
            //通过构造PageInfo对象获取分页信息，如当前页码，总页数，总条数
            PageInfo<Platform> pageInfo = new PageInfo<Platform>(list);
            return CommonResult.success(pageInfo);
        }
        return CommonResult.failed();
    }

    @ResponseBody
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    @ApiOperation("根据名字查询平台")
    public CommonResult<Platform> selectPlatformByName(@RequestParam String name){
      Platform list=platformService.selectPlatformByName(name);
        if(list!=null){
            //通过构造PageInfo对象获取分页信息，如当前页码，总页数，总条数
            return CommonResult.success(list);
        }
        return CommonResult.failed();
    }

}
